package com.sinodata.bsm.cicp.collector.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.AuthenticationFailedException;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.constants.DataTypeConstants;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: Imap采集器
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-11-12 15:00:14      liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class ImapCollector extends Collector {

    private static final Logger logger = Logger.getLogger(ImapCollector.class);

    /**
     *
     */
    public ImapCollector() {

    }

    /**
     * 参数说明: attr[0]:IMAP服务器地址 attr[1]:邮箱用户名 attr[2]:邮箱密码
     * @throws CollectorException 
     */
    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        String imapServer = this.getParamMap().get("IMAP Server Address").trim();//IMAP服务器地址
        String userName = this.getParamMap().get("Username").trim();//用户名
        String passWord = this.getParamMap().get("Password").trim();//密码
        //        propValues[0] = ResPropertyValue.createPropValue("NO", propMetas[0]);
        propValues[0] = ResPropertyValue.createPropValue(DataTypeConstants.BOOLEAN_VALUE_FALSE, propMetas[0]);
        propValues[1] = ResPropertyValue.createPropValue("", propMetas[1]);
        propertyValueX.setValues(propValues);
        values.add(propertyValueX);
        try {
            Properties props = System.getProperties();
            Session session = Session.getInstance(props, null);
            Store store = session.getStore("imap");
            store.connect(imapServer, userName, passWord);
            boolean success = store.isConnected();
            if (success) {
                //propValues[0] = ResPropertyValue.createPropValue("YES", propMetas[0]);
                propValues[0] = ResPropertyValue.createPropValue(DataTypeConstants.BOOLEAN_VALUE_TRUE, propMetas[0]);
            }
            return values;
        } catch (AuthenticationFailedException afe) {
            logger.info("User name password mistake", afe);
            //            propValues[1] = ResPropertyValueX.createPropValue("用户名密码错误", propMetas[1]);
            propValues[1] = ResPropertyValue.createPropValue("Username password error", propMetas[1]);
            return values;
        } catch (MessagingException me) {
            logger.info("IMAP server connection error", me);
            //            propValues[1] = ResPropertyValueX.createPropValue("IMAP服务器连接错误", propMetas[1]);
            propValues[1] = ResPropertyValue.createPropValue("IMAP server connection error", propMetas[1]);
            return values;
        } catch (Exception e) {
            logger.error("", e);
            throw new CollectorException(e);
        }
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "IMAP服务器地址", "用户名", "密码" };
        return new String[] { "IMAP Server Address", "Username", "Password" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "是否可用", "错误信息" };
        return new String[] { "Whether_Available", "Error_Information" };
    }

}
